   
#include "XMLConfig.h"
#include <iostream.h>

void XMLConfig::readFile(TString filename,TString name, TString variation, vector<TString> * the_files) 
{
   // First create engine
   TXMLEngine* xml = new TXMLEngine;
   // Now try to parse xml file
   // Only file with restricted xml syntax are supported
   XMLDocPointer_t xmldoc = xml->ParseFile(filename);
   if (xmldoc==0) {
      delete xml;
      return;  
   }

   // take access to main node   
   XMLNodePointer_t mainnode = xml->DocGetRootElement(xmldoc);
   
   // display recursively all nodes and subnodes
   m_currentfiles.clear();
   readNode(xml, mainnode,name,variation);
   cout << "filename: " << filename << ", name: "<< name<< ", variation:" << variation << endl ;
   for (int i = 0 ; i < m_currentfiles.size() ; i ++){
   //  cout << m_currentfiles[i] << endl ;
     the_files->push_back(m_currentfiles[i]);
   }
   
   // Release memory before exit
   xml->FreeDoc(xmldoc);
   delete xml;
}

void XMLConfig::readNode(TXMLEngine* xml, XMLNodePointer_t node,TString name,TString variation) 
{
   // this function display all accessible information about xml node and its childs
   
//   printf("node: %s\n", xml->GetNodeName(node));
   
   // display attributes
   XMLAttrPointer_t attr = xml->GetFirstAttr(node);
   while (attr!=0) {
       	     printf("attr: %s value: %s\n",xml->GetAttrName(attr), xml->GetAttrValue(attr));
       attr = xml->GetNextAttr(attr);
   }
   
   // display content (if exists)
   const char* content = xml->GetNodeContent(node);
   if (content!=0) 
      printf("cont: %s\n", content);
      
   // display all child nodes   
   XMLNodePointer_t child = xml->GetChild(node);
   while (child!=0) {
      readCollection(xml, child, name,variation); 
      child = xml->GetNext(child);
   }
}

void XMLConfig::readCollection(TXMLEngine* xml, XMLNodePointer_t node, TString name,TString variation) 
{
   // this function display all accessible information about xml node and its childs
//   printf("node: %s\n", xml->GetNodeName(node));
   
   // display attributes
   XMLAttrPointer_t attr = xml->GetFirstAttr(node);
   TString attribute ; 
   TString value ; 
   TString node_name ; 
   TString node_variation ; 
   TString node_directory ; 
   while (attr!=0) {
       	attribute = xml->GetAttrName(attr);
        value     = xml->GetAttrValue(attr);
        if (attribute == "name"){
	    node_name =  value ;
        }
        if (attribute == "variation"){
	    node_variation =  value ;
        }
        if (attribute == "directory"){
	    node_directory =  value ;
        }
       	attr = xml->GetNextAttr(attr);
   }
   //cout << "name: "      << node_name <<endl;
   //cout << "variation: " << node_variation <<endl;
   //cout << "directory: " << node_directory <<endl;

   if (node_name == name && node_variation == variation ){
	   // display content (if exists)
	   const char* content = xml->GetNodeContent(node);
	   if (content!=0) 
	      printf("cont: %s\n", content);
	      
	   // display all child nodes
	   XMLNodePointer_t child = xml->GetChild(node);
	   while (child!=0) {
	      readFiles(xml, child,  node_directory); 
	      child = xml->GetNext(child);
	   }
   }
}

void XMLConfig::readFiles(TXMLEngine* xml, XMLNodePointer_t node,TString directory) 
{
   const char* content = xml->GetNodeContent(node);
   if (content!=0) {
//      printf("cont: %s\n",  content);
      m_currentfiles.push_back(directory +content);
   }
//   for (int i = 0 ; i < file_list.size() ; i ++){
//       cout << file_list[i] << endl ;
//   }
      
}


//void test(){
//    XMLConfig * xml = new XMLConfig();
//    xml->readFile("datacollection.xml","backgroundMC","nominal");
//}
